package com.zprmb.udzt;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zprmb.udzt.bean.BankGift;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.MyReceiver;
import com.zprmb.udzt.util.VolleryUtil;

/**
 * 短信对话框
 * 
 * @author shenshao
 * 
 */
public class Message_dialog extends BaseActivity implements OnClickListener {

	private TextView tv_user_phone, countdown, tv_cancel, tv_confirm, tv_title;
	private EditText et_codes;
	private TimeCount timeCount;
	private String phone, idcard, bank_card, real_name, mobile;
	private int who, isbank;
	private List<BankGift> list = new ArrayList<BankGift>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mes_dialog);
		BbtApplication.getInstance().addActvity(this);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
						| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		phone = getIntent().getStringExtra("user_phone");
		who = getIntent().getIntExtra("who", 0);
		IntentFilter mFilter = new IntentFilter();
		mFilter.addAction("exit");
		this.registerReceiver(myReceiver, mFilter);
		tv_cancel = (TextView) findViewById(R.id.tv_cancel);
		tv_confirm = (TextView) findViewById(R.id.tv_confirm);
		tv_user_phone = (TextView) findViewById(R.id.user_phone);
		countdown = (TextView) findViewById(R.id.tv_countdown);
		et_codes = (EditText) findViewById(R.id.et_codes);
		tv_title = (TextView) findViewById(R.id.bind_title);
		tv_cancel.setOnClickListener(this);
		tv_confirm.setOnClickListener(this);
		countdown.setOnClickListener(this);
		isbank = getIntent().getIntExtra("isbank", 0);
		if (isbank == 1) {
			tv_title.setText("信息确认");
		} else {
			tv_title.setText("绑卡验证");
		}
		mobile = phone;
		String maskNumber = mobile.substring(0, 3) + "****"
				+ mobile.substring(7, mobile.length());
		tv_user_phone
				.setText(Html
						.fromHtml("<font color='#cccccc'>本次交易需要短信确认,校验码已<br>发送到您的手机</font>"
								+ "<font color='#00aaee'>"
								+ maskNumber
								+ "</font>"));
		timeCount = new TimeCount(60000, 100);
		timeCount.start();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}

	MyReceiver myReceiver = new MyReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			finish();
		}
	};

	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {// 计时完毕
			countdown.setText("重新获取");
			countdown.setClickable(true);
			countdown.setBackgroundResource(R.drawable.my_button);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程
			countdown.setClickable(false);// 防止重复点击
			countdown.setText(millisUntilFinished / 1000 + "s后重新获取");
			countdown.setBackgroundResource(R.drawable.btn_unclick);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_cancel:
			finish();
			break;
		case R.id.tv_confirm:
			if (et_codes.getText().toString().equals("")) {
				Toast.makeText(Message_dialog.this, "请输入验证码", 2000).show();
			} else {

				confirm();
			}
			break;
		case R.id.tv_countdown:
			again();
			timeCount.start();
			break;
		}

	}

	private void again() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(1);

		volleryUtil.execute(Message_dialog.this, Constant.URL
				+ "/api/v5/account/bankcard/bind/request/again?", "");
	}

	public static String[] convertStrToArray(String str) {
		String[] strArray = null;
		strArray = str.split(","); // 拆分字符为"," ,然后把结果交给数组strArray
		return strArray;
	}

	private void confirm() {
		// TODO Auto-generated method stub
		AjaxParams params = new AjaxParams();
		params.put("captcha", et_codes.getText().toString());
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("ss", jsonObject.toString());
				JSONObject json1 = jsonObject.optJSONObject("data");
				if (jsonObject.optInt("success") == 1) {
					Gson gson = new Gson();
					list = gson.fromJson(jsonObject.optString("resultList"),
							new TypeToken<ArrayList<BankGift>>() {
							}.getType());
					if (getIntent().getBooleanExtra("flag", true)) {
						if (isbank != 1) {
							Intent intent1 = new Intent(Message_dialog.this,
									BindCard_Success.class);
							intent1.putExtra("flag", getIntent()
									.getBooleanExtra("flag", true));
							intent1.putExtra("coupon1", convertStrToArray(list
									.get(0).getINTEREST_RATES())[0]);
							intent1.putExtra("rate1", convertStrToArray(list
									.get(0).getINTEREST_RATES())[1]);
							intent1.putExtra("coupon2", convertStrToArray(list
									.get(1).getINTEREST_RATES())[0]);
							intent1.putExtra("rate2", convertStrToArray(list
									.get(1).getINTEREST_RATES())[1]);
							intent1.putExtra("coupon3", convertStrToArray(list
									.get(2).getINTEREST_RATES())[0]);
							intent1.putExtra("rate3", convertStrToArray(list
									.get(2).getINTEREST_RATES())[1]);
							intent1.putExtra("who", who);

							startActivity(intent1);
							Message_dialog.this.finish();
							// if(!json1.optString("bindcardAmount").equals("")&&json1.optString("bindcardAmount")!=null){
							//
							// // intent1.putExtra("tiyan",
							// json1.optString("bindcardAmount"));
							//
							// }

						} else {
							Intent intent1 = new Intent(Message_dialog.this,
									Userinfo_success.class);
							startActivity(intent1);
							Message_dialog.this.finish();
						}

					} else {
						Intent intent1 = new Intent(Message_dialog.this,
								BindCard_Success.class);
						intent1.putExtra("flag",
								getIntent().getBooleanExtra("flag", true));
						// intent1.putExtra("tiyan",
						// json1.optString("bindcardAmount"));
						// intent1.putExtra("auth",
						// json1.optString("authenticationAmount"));
						intent1.putExtra("flag",
								getIntent().getBooleanExtra("flag", true));
						intent1.putExtra("coupon1",
								convertStrToArray(list.get(0)
										.getINTEREST_RATES())[0]);
						intent1.putExtra("rate1", convertStrToArray(list.get(0)
								.getINTEREST_RATES())[1]);
						intent1.putExtra("coupon2",
								convertStrToArray(list.get(1)
										.getINTEREST_RATES())[0]);
						intent1.putExtra("rate2", convertStrToArray(list.get(1)
								.getINTEREST_RATES())[1]);
						intent1.putExtra("coupon3",
								convertStrToArray(list.get(2)
										.getINTEREST_RATES())[0]);
						intent1.putExtra("rate3", convertStrToArray(list.get(2)
								.getINTEREST_RATES())[1]);
						intent1.putExtra("who", who);
						Log.e("time1", convertStrToArray(list.get(0)
								.getINTEREST_RATES())[1] + "---123");
						Log.e("time2", convertStrToArray(list.get(1)
								.getINTEREST_RATES())[1] + "---123");
						Log.e("time3", convertStrToArray(list.get(2)
								.getINTEREST_RATES())[1] + "---123");
						Log.e("集合1", list.get(0).getINTEREST_RATES());
						Log.e("集合2", list.get(1).getINTEREST_RATES());
						Log.e("集合3", list.get(2).getINTEREST_RATES());
						startActivity(intent1);
						Message_dialog.this.finish();
					}
				} else {
					if (jsonObject.optString("comment").equals("please login")) {
						Intent intent = new Intent(Message_dialog.this,
								LoginActivity.class);
						startActivity(intent);
					} else {
						Toast.makeText(Message_dialog.this,
								jsonObject.optString("comment"), 2000).show();
					}
				}

			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(1);
		volleryUtil.setShow(true);
		volleryUtil.execute(Message_dialog.this, Constant.URL
				+ "/api/v5/account/bankcard/bind/request/confirm?",
				params.toString());

	}
}
